package com.witframework.components.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.witframework.PropertiesConstants;
import com.witframework.core.ApplicationProperties;

public class CustomSelectTreeDialogTag extends TagSupport{
	protected String treeid;//选择组件的ID
	protected String data;//数据源，可以是实体的类名或数据库的表名，或是sql查询语句
	protected String data2;//数据源，可以是实体的类名或数据库的表名，或是sql查询语句	
	protected String root;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性
	protected String parentid;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性
	protected String parentid2;
	protected String field1;//页面域的id,一般最少需要选择一项数据到目标页面
	protected String nodeid;//对应的熟悉名称(树节点的ID)，对于PAGE类型则忽略该属性
	protected String nodeid2;
	protected String field2;//第二个页面输入域的id
	protected String nodelabel;//上面对应的数据字段名称(树节点的label文本)，对于PAGE类型则忽略该属性
	protected String nodelabel2;
	protected String field3;//第三个页面输入域的id，默认可以有最多三个数据项可以从选择对话框选取。
	protected String others;//如果需要选取超过三个的数据项，可以设置该属性值为多个字段名,如：email,phone，对于PAGE类型则忽略该属性
	
	protected String dialogTitle;//对话框的标题
	protected String icon="/webresource/images/ico_btn_search.gif";//图标
	protected String width;//对话框宽度
	protected String height;//对话框高度
	/**
	 * 数据源类型,table:数据库表；class:实体类名(通过对应的service查找数据),默认的数据源类型；
	 * sql:sql 查询语句.如果为sql类型，则filters属性的设置是无效的
	 */
	protected String datatype="class";	
	protected String multiple;//是否允许多选，对于PAGE类型则忽略该属性
	protected String selected;//尚未完全实现，默认的选中项
	
	protected String onlyLeaf="true";
	
	/**
	 * 数据过滤条件，对于class类型的如：eq:catalog,like:className:A,between:id 
	 * 对于table类型的如:"catalog='system' and name like 'witframework.%'"
	 * 对于PAGE、SQL类型，则忽略该属性
	 */
	private String filters;
	
	private String show="false";
	private String top="0";
	private String left="0";
	
	


	@Override
	public int doStartTag() throws JspException {		
		StringBuffer dataUrl=new StringBuffer("customSelectTreeController!treepage"+ApplicationProperties.getProperty(PropertiesConstants.WIT));//数据列表的URL
		if(StringUtils.isNotEmpty(datatype)&&datatype.equalsIgnoreCase("page")){
			dataUrl=new StringBuffer(data);
		}
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		JspWriter writer = pageContext.getOut();	
		
		if(icon.startsWith("/")&&!icon.startsWith(request.getContextPath())) icon=request.getContextPath()+icon;
		
		if(dataUrl.toString().startsWith("/")&&!dataUrl.toString().startsWith(request.getContextPath())) dataUrl=new StringBuffer(request.getContextPath()+dataUrl);
		dataUrl.append((dataUrl.indexOf("?")>0?"&":"?")+"treeid="+treeid+"&data="+getData());
		if(StringUtils.isNotEmpty(root)) dataUrl.append("&root="+root);
		if(StringUtils.isNotEmpty(datatype)||!datatype.equalsIgnoreCase("page")){	
			if(StringUtils.isNotEmpty(data2)) dataUrl.append("&data2="+getData2());
			if(StringUtils.isNotEmpty(parentid)) dataUrl.append("&parentid="+parentid);
			if(StringUtils.isNotEmpty(parentid2)) dataUrl.append("&parentid2="+parentid2);
			if(StringUtils.isNotEmpty(nodeid)) dataUrl.append("&nodeid="+nodeid);
			if(StringUtils.isNotEmpty(nodeid2)) dataUrl.append("&nodeid2="+nodeid2);
			if(StringUtils.isNotEmpty(nodelabel))	dataUrl.append("&nodelabel="+nodelabel);
			if(StringUtils.isNotEmpty(nodelabel2))	dataUrl.append("&nodelabel2="+nodelabel2);
			if(StringUtils.isNotEmpty(others))	dataUrl.append("&others="+others);	
			if(StringUtils.isNotEmpty(selected))	dataUrl.append("&selected="+selected);				
			if(StringUtils.isNotEmpty(filters)) 	dataUrl.append("&filters="+getFilters());		
			if(StringUtils.isNotEmpty(datatype)) dataUrl.append("&datatype="+datatype);			
		}		
		if(StringUtils.isNotEmpty(multiple))	dataUrl.append("&multiple="+multiple);	
		if(StringUtils.isNotEmpty(onlyLeaf))	dataUrl.append("&onlyLeaf="+onlyLeaf);	
		if(StringUtils.isNotEmpty(height))	dataUrl.append("&height="+height);
		if(StringUtils.isNotEmpty(width))	dataUrl.append("&width="+width);		
		
		if(StringUtils.isEmpty(dialogTitle)) dialogTitle="对话框";
		
		StringBuffer html=new StringBuffer();
		if(show.equalsIgnoreCase("true")||show.equals("1")){
			html.append("<script>showIframe(");
		}else{
			html.append("<a class=\"\" href=\"#\" onclick=\"showSelectWin(");
		}
		
		html.append("'"+treeid+"','"+"<img class=&quot;select_img&quot; src=&quot;"+icon+"&quot; />"
				+dialogTitle+"','"+dataUrl);		
		html.append("','");		
		if(StringUtils.isNotEmpty(field1))	html.append("t1="+field1+";");
		if(StringUtils.isNotEmpty(field2))	html.append("t2="+field2+";");
		if(StringUtils.isNotEmpty(field3))	html.append("t3="+field3+";");
		html.append("'");		
		if(StringUtils.isNotEmpty(width)){
			html.append(",'',"+width);
		}else{
			html.append(",'',''");
		}
		if(StringUtils.isNotEmpty(height)){
			html.append(","+height);
		}else{
			html.append(",''");
		}
		if(StringUtils.isNotEmpty(top)){
			html.append(","+top);
		}else{
			html.append(",'0'");
		}
		if(StringUtils.isNotEmpty(left)){
			html.append(","+left);
		}else{
			html.append(",'0'");
		}
		
		if(show.equalsIgnoreCase("true")||show.equals("1")){
			html.append(");</script>");
		}else{
			html.append(");\" >");
			html.append("<img class=\"select_img\" src=\""+icon+"\"/>");
			html.append("</a>");
		}
		
				
		try {			
			writer.println(html);
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		return (EVAL_BODY_INCLUDE);
	}

	

	public void setIcon(String icon) {
		this.icon = icon;		
		try {
			this.icon=(String) ExpressionEvaluatorManager.evaluate("icon",icon,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}

	

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}

	public void setSelected(String selected) {
		this.selected = selected;		
		try {
			this.selected=(String) ExpressionEvaluatorManager.evaluate("selected",selected,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setData(String data) {
		this.data = data;
		try {
			this.data=(String) ExpressionEvaluatorManager.evaluate("data",data,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}



	public void setData2(String data2) {
		this.data2 = data2;
		try {
			this.data2=(String) ExpressionEvaluatorManager.evaluate("data2",data2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}



	public void setField1(String field1) {
		this.field1 = field1;
		try {
			this.field1=(String) ExpressionEvaluatorManager.evaluate("field1",field1,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}



	public void setNodeid(String nodeid) {
		this.nodeid = nodeid;
	}



	public void setField2(String field2) {
		this.field2 = field2;
		try {
			this.field2=(String) ExpressionEvaluatorManager.evaluate("field2",field2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}



	public void setNodelabel(String nodelabel) {
		this.nodelabel = nodelabel;
	}



	public void setField3(String field3) {
		this.field3 = field3;
		try {
			this.field3=(String) ExpressionEvaluatorManager.evaluate("field3",field3,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}



	public void setOthers(String others) {
		this.others = others;
	}



	public void setDialogTitle(String dialogTitle) {
		this.dialogTitle = dialogTitle;
	}



	public void setFilters(String filters) {
		this.filters = filters;
		try {
			this.filters=(String) ExpressionEvaluatorManager.evaluate("filters",filters,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
		
	}



	public void setParentid(String parentid) {
		this.parentid = parentid;
	}



	public void setRoot(String root) {
		this.root = root;
	}



	public void setTreeid(String treeid) {
		this.treeid = treeid;
		try {
			this.treeid=(String) ExpressionEvaluatorManager.evaluate("treeid",treeid,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}



	public void setOnlyLeaf(String onlyLeaf) {
		this.onlyLeaf = onlyLeaf;
	}



	public void setNodeid2(String nodeid2) {
		this.nodeid2 = nodeid2;
	}



	public void setNodelabel2(String nodelabel2) {
		this.nodelabel2 = nodelabel2;
	}



	public String getTreeid() {
		return treeid;
	}



	public String getData() {
		try {
			return java.net.URLEncoder.encode(data,"utf-8");
		} catch (Exception e1) {			
		}
		return data;
	}



	public String getData2() {
		try {
			return java.net.URLEncoder.encode(data2,"utf-8");
		} catch (Exception e1) {			
		}
		return data2;
	}



	public String getRoot() {
		return root;
	}



	public String getParentid() {
		return parentid;
	}



	public String getField1() {
		return field1;
	}



	public String getNodeid() {
		return nodeid;
	}



	public String getNodeid2() {
		return nodeid2;
	}



	public String getField2() {
		return field2;
	}



	public String getNodelabel() {
		return nodelabel;
	}



	public String getNodelabel2() {
		return nodelabel2;
	}



	public String getField3() {
		return field3;
	}



	public String getOthers() {
		return others;
	}



	public String getDialogTitle() {
		return dialogTitle;
	}



	public String getIcon() {
		return icon;
	}



	public String getWidth() {
		return width;
	}



	public String getHeight() {
		return height;
	}



	public String getDatatype() {
		return datatype;
	}



	public String getMultiple() {
		return multiple;
	}



	public String getSelected() {
		return selected;
	}



	public String getOnlyLeaf() {
		return onlyLeaf;
	}



	public String getFilters() {
		try {
			return java.net.URLEncoder.encode(filters,"utf-8");
		} catch (Exception e1) {			
		}
		return filters;
	}



	public String getShow() {
		return show;
	}



	public void setShow(String show) {
		this.show = show;
	}
	public void setTop(String top) {
		this.top = top;
	}

	public void setLeft(String left) {
		this.left = left;
	}



	public void setParentid2(String parentid2) {
		this.parentid2 = parentid2;
	}



}
